import Vue from 'vue';
import VueScroller from 'vue-scroller';
import ls from 'store2'
import VueLazyload from 'vue-lazyload'
Vue.use(VueScroller)
Vue.use(VueLazyload)
window.document.body.addEventListener('touchmove', function(e) {
	e.preventDefault();
}, {
	passive: false
});

var commonJs = {
	install(Vue) {
		Vue.prototype.commonJs = {
			aback: function(routerName, queryData) {

				let that = this
				let abc = this.funs(routerName, queryData)
				if (window.history && window.history.pushState) {

					history.pushState(null, null, document.URL);

					window.addEventListener('popstate', abc, false); //false阻止默认事件
				}



			},
			funs: function(routerName, queryData) {

				window.$nuxt.$router.replace({
					path: routerName,
					query: {
						type: queryData
					}
				})


			},
			destr: function() {
				window.removeEventListener('popstate', this.backuniv, false);
			},
			xahui: function(routerName, queryData) {
				let that = this
				let abc = this.funs(routerName, queryData)
				window.removeEventListener('popstate', abc, false); //false阻止默认事件

			},
			returnProject: function(url) {
				if (ls.get('tokens') === null || ls.get('yxkData') == null) {
					
					window.location.replace(process.env.workUrl+'/wx-yxk/#/chooseIdentity?projct_urls='+encodeURIComponent(process.env.workUrl+'/h5-stu-wx/#/'+url)+'&type=users')
					// window.$nuxt.$router.push('../teachers/tealogin')
					return false;
				}
			},
			gologins: function() {
				if (ls.get('tokens') === null || ls.get('yxkData') == null) {
					// window.$nuxt.$router.push('../teachers/tealogin')
					window.$nuxt.$router.replace({
						path: '../users/login'
					})


					return false;
				}
			},
			gosback: function(routerName, queryData) {
				window.$nuxt.$router.go(-1)

			},
			gosto: function(routerName, queryData) {

				window.$nuxt.$router.replace({
					path: routerName,
					query: {
						type: queryData
					}
				})

			},
			gopush: function(routerName, queryData) {

				window.$nuxt.$router.push({
					path: routerName,
					query: {
						type: queryData
					}
				})

			},
			blurAdjust(e) {
				window.scroll(0,0)
				// setTimeout(() => {
				// 	if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
				// 		return
				// 	}

				// 	let result = 'pc';
				// 	if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
				// 		result = 'ios'
				// 	} else if (/(Android)/i.test(navigator.userAgent)) { //判断Android
				// 		result = 'android'
				// 	}

				// 	// if (result = 'ios') {
				// 		document.activeElement.scrollIntoViewIfNeeded(true);
				// 	// }
				// }, 20)
			},
			isTerminal() {
				var u = navigator.userAgent;
				var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
				var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
				if (isAndroid) {
					return "isAndroid"
				} else if (isiOS) {
					return "isiOS"
				}
			},
			GetUrlParam(paraName) {
				var url = document.location.toString();
				var arrObj = url.split("?");
				if (arrObj.length > 1) {
					var arrPara = arrObj[1].split("&");
					var arr;

					for (var i = 0; i < arrPara.length; i++) {
						arr = arrPara[i].split("=");

						if (arr != null && arr[0] == paraName) {
							return arr[1];
						}
					}
					return "";
				} else {
					return "";
				}
			},

			checkCodeVip(proCode, return_url, id) {

				if (this.checkTerminal() === 'ios') {
					let obj = {
						"gourl": process.env.mallUrl + '/products/' + id,
						"pay_return_url": return_url
					}

					window.webkit.messageHandlers.goZyMal.postMessage(obj)
				} else if (this.checkTerminal() === 'android') {
					let obj = {
						"gourl": process.env.mallUrl + '/products/' + id,
						"pay_return_url": return_url
					}
					let objs = JSON.stringify(obj)
					ZMParentsJsToAndroid.goZyMal(objs)
				} else {
					window.location.href = process.env.mallUrl + '/products/' + id +
						"?pay_return_url=" + return_url
				}
			},
			checkTerminal() {
				var u = navigator.userAgent
				var chrome = u.match(/Chrome\/([\d.]+)/) || u.match(/CriOS\/([\d.]+)/)
				var isWechat = u.toLowerCase().match(/MicroMessenger/i) == 'micromessenger' || u.match(/_SQ_/i) == '_sq_'
				var isAndroid = u.match(/(Android);?[\s\/]+([\d.]+)?/)
				var isiOS = !chrome && u.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/)
				if (isWechat) {
					return 'wechat'
				} else if (isiOS) {
					return 'ios'
				} else if (isAndroid) {
					return 'android'
				} 
			},
			accectAppParame(){
				if (this.checkTerminal() === "ios") {
					let iOSInfos = window.iOSInfo;
					if (Object.keys(iOSInfos).length > 0){
						ls.set("yxkData", iOSInfos);
						ls.set("userType", iOSInfos.userType);
						ls.set("tokens", iOSInfos.tokens);
						ls.set("userArea", iOSInfos.userArea);
					}
		
			} else if (this.checkTerminal() === "android") {
				let data = window.ZMParentsJsToAndroid.getZyMsg();
				let androidData = JSON.parse(data);
				if (androidData.userType) {
					ls.set("yxkData", androidData);
					ls.set("userType", androidData.userType);
					ls.set("tokens", androidData.tokens);
					ls.set("userArea", androidData.userArea);
				}
			}
			 else {
				let wxurl = window.location.href;
				this.jumpUrl = decodeURIComponent(
					this.GetUrlParam("tea_return_url")
				);
				if (this.jumpUrl) {
					ls.set("jumpsTeaUrl", this.jumpUrl);
					let datas = this.GetUrlParam("tea_userData");
					let usersData = JSON.parse(decodeURIComponent(datas));
					ls.set("yxkData", usersData);
					ls.set("userType", usersData.userType);
					ls.set("tokens", usersData.accessToken);
				}
			}
		}


	};

	}

}
Vue.use(commonJs)
